package com.itheima.iterator;

/**
 * @author zzw
 * @date 2025/10/26
 * @description 迭代器模式测试类
 */
public class Client {
    public static void main(String[] args) {

        // 创建聚合对象
        StudentAggregateImpl aggregate = new StudentAggregateImpl();

        // 添加元素
        Student student1 = new Student("张三", "001");
        aggregate.addStudent(student1);
        Student student2 = new Student("李四", "002");
        aggregate.addStudent(student2);
        Student student3 = new Student("王五", "003");
        aggregate.addStudent(student3);
        Student student4 = new Student("赵六", "004");
        aggregate.addStudent(student4);

        // 遍历聚合对象
        // 1.获取迭代器对象
        StudentIterator iterator = aggregate.getStudentIterator();
        // 2.遍历
        while (iterator.hasNext()) {
            // 3.获取元素
            Student student = iterator.next();
            System.out.println(student.toString());
        }

        System.out.println("======");

        // 移除元素
        aggregate.removeStudent(student4);
        // 重新获取迭代器对象
        StudentIterator newIterator = aggregate.getStudentIterator();
        while (newIterator.hasNext()) {
            // 获取元素
            Student student = newIterator.next();
            System.out.println(student.toString());
        }
    }
}
